package sk.openit.xml.jaxp.dom;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import sk.openit.xml.entity.DbConfigTags;

public class DbConfigReaderEchoOnly implements DbConfigTags {
	private Document doc;
	private String fileName;

	public DbConfigReaderEchoOnly(String fileName) {
		this.fileName = fileName;
	}

	public void process() {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

		try {
			DocumentBuilder builder = factory.newDocumentBuilder();
			doc = builder.parse(new File(fileName));
			doc.getDocumentElement().normalize();

			System.out.print("Root element :"	+ doc.getDocumentElement().getNodeName());
			System.out.println(" (type = " + doc.getDocumentElement().getAttribute(DB_TYPE) + ")");
			System.out.println("--------------------------------------");

			NodeList nList = doc.getElementsByTagName(CONFIG); //config

			for (int index = 0; index < nList.getLength(); index++) { //configs
				Node nNode = nList.item(index);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					
					Element eElement = (Element) nNode;
					NodeList childList = eElement.getChildNodes();
					for (int index2 = 0; index2 < childList.getLength(); index2++) {
						Node childNode = childList.item(index2);
						if (childNode.getNodeType() == Node.ELEMENT_NODE) {
							Element childElement = (Element) childNode;
							System.out.println("Tag: " + childElement.getTagName());
							
							NodeList dataList = childNode.getChildNodes();
							for (int index3 = 0; index3 < dataList.getLength(); index3++) {
								Node dataNode = dataList.item(index3);
								if (dataNode.getNodeType() == Node.TEXT_NODE) {
									System.out.println("Value: " + dataNode.getNodeValue());									
								}								
							}							
						}						
					}

//					System.out.println(HOST + " : "	+ getTagValue(HOST, eElement));
//					System.out.println(PORT + " : "	+ getTagValue(PORT, eElement));
//					System.out.println(INSTANCE + " : "	+ getTagValue(INSTANCE, eElement));
//					System.out.println(DATABASE + " : "	+ getTagValue(DATABASE, eElement));
//					System.out.println(USER + " : "	+ getTagValue(USER, eElement));
//					System.out.println(PASSWORD + " : "	+ getTagValue(PASSWORD, eElement));
				}
			}
		} catch (SAXException spe) {
			System.err.println("Unable to parse XML structure: " + fileName);
			spe.printStackTrace();
		} catch (ParserConfigurationException pce) {
			System.err.println("Wrong SAXParser configuration");
			pce.printStackTrace();
		} catch (IOException ioe) {
			System.err.println("Unable to open xml file: " + fileName);
			ioe.printStackTrace();
		}
	}

	private String getTagValue(String sTag, Element eElement) {
		NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
		Node nValue = (Node) nlList.item(0);
		return nValue.getNodeValue();
	}
}
